<template>
  <div class="publish-container">
    <van-nav-bar title="发布视频" :border="false" :fixed="true" />

    <div class="publish-content">
      <van-form @submit="onSubmit" validate-trigger="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="formData.title"
            name="title"
            label="标题"
            placeholder="请输入视频标题"
            :rules="[{ required: true, message: '请输入标题' }]"
            maxlength="50"
            show-word-limit
          />

          <van-field
            v-model="formData.description"
            name="description"
            label="描述"
            type="textarea"
            rows="3"
            placeholder="添加视频描述..."
            maxlength="200"
            show-word-limit
          />
        </van-cell-group>

        <div class="upload-wrap">
          <VideoUpload
            :formData="formData"
            @success="handleUploadSuccess"
            @error="handleUploadError"
            :maxSize="1024 * 1024 * 500"
          />
        </div>

        <div class="submit-btn">
          <van-button round block type="primary" native-type="submit" :disabled="!videoId">
            发布
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { videoApi } from '@/api/video'
import VideoUpload from '@/components/VideoUpload.vue'

const router = useRouter()
const videoId = ref('')
const uploadInfo = ref(null)
const formData = ref({
  title: '',
  description: '',
})

const handleUploadSuccess = (info) => {
  videoId.value = info.videoId
  uploadInfo.value = info
  if (!formData.value.title) {
    const fileName = info.fileName || ''
    formData.value.title = fileName.replace(/\.[^/.]+$/, '')
  }
}

const handleUploadError = (error) => {
  console.error('上传失败：', error)
  videoId.value = ''
  uploadInfo.value = null
}

const onSubmit = async () => {
  if (!videoId.value || !uploadInfo.value) {
    showToast('请先上传视频')
    return
  }
  try {
    await videoApi.createVideo({
      vodId: videoId.value,
      title: formData.value.title.trim(),
      description: formData.value.description.trim(),
    })
    showToast('发布成功')
    router.push('/')
  } catch (error) {
    showToast('发布失败：' + error.message)
  }
}
</script>

<style scoped>
.publish-container {
  min-height: 100vh;
  background-color: var(--container-background-color);
  padding-top: 46px;
}

.publish-content {
  padding: 16px;
}

:deep(.van-nav-bar) {
  background-color: transparent;
}

:deep(.van-nav-bar__title) {
  color: var(--font-color-base);
  font-weight: 600;
}

:deep(.van-cell-group--inset) {
  margin: 0;
  background-color: transparent;
}

:deep(.van-field) {
  background-color: #fff;
}

.upload-wrap {
  margin: 16px 0;
  padding: 16px;
  background-color: #fff;
  border-radius: 8px;
}

.submit-btn {
  margin: 24px 16px;
}

:deep(.van-button--primary) {
  background: linear-gradient(to right, #fe2c55, #ff3b30);
  border: none;
}

:deep(.van-button--disabled) {
  opacity: 0.6;
}

:deep(.van-field__word-limit) {
  color: #999;
}
</style>
